package com.grts.mall.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.grts.mall.mapper.AdminMapper;
import com.grts.mall.pojo.Admin;
import com.grts.mall.pojo.AdminExample;

@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminMapper adminMapper;
	
	@Override
	public void save(Admin admin) {
		this.adminMapper.insert(admin);
	}

	@Override
	public boolean login(Admin admin) {
		// 密码散列处理
		String md5DigestAsHex = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()).toUpperCase();
		admin.setPassword(md5DigestAsHex);
		// 构造查询条件
		AdminExample example = new AdminExample();
		example
			.createCriteria()
			.andAdminNameEqualTo(admin.getAdminName())
			.andPasswordEqualTo(admin.getPassword());
		// 调用mapper进行查询
		List<Admin> list = this.adminMapper.selectByExample(example);
		// 如果查无结果, 说明登录失败
		if (list == null || list.size() == 0) {
			return false;
		}
		// 查有结果, 把adminId拿到, 放到参数对象admin中, 这样, Controller可以得到参数admin中的adminId信息, 可以往session中存.
		admin.setAdminId(list.get(0).getAdminId());
		return true;
	}

}
